package com.sijobe.installer;

import java.io.OutputStream;
import java.io.PrintStream;

/**
 * Basic wrapper that goes over System.out to allow the installer GUI get all
 * System.out messages.
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @author simo_415
 */
public class InstallOutputStream extends PrintStream {

   public static final int DEBUG = 1;        // Line starts with DEBUG
   public static final int INFO = 2;         // Line can start with anything, except DEBUG
   public static final int WARNING = 3;      // Line starts with WARNING
   public static final int ERROR = 4;        // Line starts with ERROR
   public static final int DISABLED = 5;     // Disables logging
   
   private int level;
   
   public InstallOutputStream(OutputStream out, int level) {
      super(out);
      this.level = level;
   }

   public void println() {
      if (level < DISABLED) {
         super.println();
         GuiMain.log("");
      }
   }
   
   public void println(String line) {
      if (level < DISABLED && line != null) {
         if (line.toUpperCase().startsWith("ERROR")) { // print error
            out(line);
         } else if (line.toUpperCase().startsWith("WARNING") && level < ERROR) { // print warning
            out(line);
         } else if ((!line.toUpperCase().startsWith("DEBUG") && level == INFO) || level == DEBUG) { // print info
            out(line);
         } else if (line.toUpperCase().startsWith("DEBUG") && level == DEBUG) { // print debug
            out(line);
         } 
      }
   }
   
   private void out(String line) {
      super.println(line);
      GuiMain.log(line);
   }
}
